package com.qdlc.p2p.biz.processor.task.notices;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.processor.event.WineCouponsPayNoticeEvent;
import com.qdlc.p2p.biz.processor.task.AbstractTaskProcessor;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.common.constant.TradeStatus;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.common.util.LogFactory;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * 酒券兑现
 *
 * @author ygw
 */
@Service
public class WineCouponsPayNoticeProcessor extends AbstractTaskProcessor {
    protected static final Logger logger = LogFactory.TRADE_LOGGER;

    @Resource
    @Qualifier("wineCouponsPayService")
    private CollectPayService wineCouponsPayService;

    @Override
    public boolean supports(ValueEvent event) {
        return event instanceof WineCouponsPayNoticeEvent;
    }

    @Override
    public void process0(ValueEvent event) throws TaskProcessorException {
        WineCouponsPayNoticeEvent noticeEvent = (WineCouponsPayNoticeEvent) event;

        TradeStatus tradeStatus = noticeEvent.getTradeStatus(); // 交易状态
        double amount = noticeEvent.getDepositAmount(); // 投标金额
        TppTradeLog tppTradeLog = noticeEvent.getTppTradeLog(); // 交易log

        String tradeStatusStr = tradeStatus.getStatus();
        logger.debug(MessageFormat.format("S 酒券兑现代付通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
        if (tradeStatus == TradeStatus.TRADE_STATUS_FINISHED) {
            wineCouponsPayService.tradeFinish(tppTradeLog, tradeStatusStr, amount);
        } else if (tradeStatus == TradeStatus.TRADE_STATUS_FAILED) {
            wineCouponsPayService.tradeFail(tppTradeLog, tradeStatusStr);
        }
        logger.debug(MessageFormat.format("E 酒券兑现代付通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
    }
}